package com.learn.lb.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * @author laibo
 * @since 2019/8/26 14:17
 * ===================================
 * <p>kafka 生产者
 * ===================================
 */
public class KafkaProducerDemo {

    public static void main(String[] args) {
        Properties producerProp = new Properties();
        producerProp.put("bootstrap.servers", KafkaPropertiory.BROKER_LIST);
        producerProp.put("acks", "1");
        producerProp.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        producerProp.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer<String, String> producer = new KafkaProducer<>(producerProp);
        ProducerRecord<String, String> record = new ProducerRecord<>(KafkaPropertiory.TOPIC, "messageKey", "messageValue");
        while (true) {
            producer.send(record);
            System.out.println("****** 发送消息成功 *****");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
